<?php
namespace app\api\controller\salesman;

use think\Controller;
use think\Request;
use think\Db;
use app\common\model\power\Rolepower;
use app\common\model\user\User as UserModel;
use app\common\model\factory\Factory as FactoryModel;
use app\api\controller\Base;
use think\facade\Env;
use app\api\controller\Pub;
class Factory extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->is_login();
        Rolepower::powerguard('305003'); // 工厂管理
    }

    private function is_login()
    {
        $data =UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
    
    //工厂列表
    public function factoryList()
    {
        $factoryData=Db::name('factory')->select();
        if($factoryData){
            return myok($factoryData);
        }else{
            return myerr('工厂列表为空');
        }
    }

    public function factoryAdd()
    {
        $data=input('post.');
        if (empty($data['name']) || empty($data['address'])) {
            return myerr('参数不完整');
        }
        $res=Db::name('factory')->insert($data);
        if($res){
            return myok('工厂添加成功');
        }else{
            return myerr('工厂添加失败');
        }
    }

    public function factoryEdit()
    {
        $data=input('post.');
        if (empty($data['id']) || empty($data['name']) || empty($data['address'])) {
            return myerr('参数不完整');
        }
        $res=Db::name('factory')->where('id',$data['id'])->update($data);
        if($res){
            return myok('工厂修改成功');
        }else{
            return myerr('工厂修改失败');
        }
    }

    public function factoryDel()
    {
        $factoy_id=input('id');
        if (empty($factoy_id)) {
           return myerr('参数不完整');
        }
        FactoryModel::destroy($factoy_id);
        return myok('工厂删除成功');
    }

    //添加工厂管理员
    public function addFactoryAdmin()
    {
        $data=input('post.');
        if(empty($data['factory_id']) || empty($data['Iphone'] || empty($data['Name'] || empty($data['sex'])))){
                return myerr('参数不完整');
        }
        $user = new UserModel();
        $user->Iphone = $data['Iphone'];
        $user->Name = $data['Name'];
        $user->Sex = $data['sex'];
        $user->Role = 301; // 工厂管理员
        $user->State = UserModel::STATEOK;
        $user->save();
        Db::name('user_factory')->insert([
            'user_id' => $user->UserId,
            'factory_id' => $data['factory_id'],
        ]);
        return myok('工厂管理员添加成功');
    }

    //工厂管理员列表
    public function factoryAdminList(Request $request)
    {
        $page = $request->get('page') ?? 1;
        $pageSize = $request->get('pageSize') ?? 10;
        $keyword = $request->get('keyword');
        
        $factoryAdminData = Db::name('user')
            ->alias('u')
            ->join('user_factory uf', 'u.UserId = uf.user_id', 'LEFT')
            ->join('factory f', 'uf.factory_id = f.id', 'LEFT')
            ->where('u.Role', 303) 
            ->when($keyword, function ($query) use ($keyword) {
                $query->where('u.Name', 'like', "%{$keyword}%");})
            ->field([
                'u.UserId',
                'u.Name as username',
                'u.Iphone',
                'f.name as factory_name',
                'f.address as factory_address',
            ])
            ->paginate([
                'list_rows' =>$pageSize,
                'page' => $page
            ]);
        return myok($factoryAdminData);
    }

}  